package com.yjd.comm.fs.file.impl;

import com.yjd.comm.fs.file.IFilePathResolve;
import com.yjd.comm.fs.file.IFileService;
import com.yjd.comm.fs.file.IUpWrap;
import com.yjd.comm.fs.file.model.FileDownModel;
import com.yjd.comm.fs.file.model.FileUpModel;
import com.yjd.comm.fs.model.FileGroupModel;
import com.yjd.comm.fs.model.FsFileModel;
import com.yjd.comm.fs.model.FsServiceModel;
import com.yjd.comm.fs.service.IFileGroupRefService;
import com.yjd.comm.fs.service.IFileGroupService;
import com.yjd.comm.fs.service.IFsFileService;
import com.yjd.comm.fs.service.IFsServiceService;
import com.yjd.comm.fs.util.FileUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.vo.Constant.FsTypeEnum;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件服务
 * 
 * @author ready
 * 
 */
public class FileService implements IFileService {

	private Logger logger = Logger.getLogger(FileService.class);

	@Override
	public FsFileModel uploadFile(FileUpModel fileUpModel) throws Exception {
		FsFileModel fsFileModel = null;
		logger.debug("上传文件 start");
		if (fileUpModel != null) {
			fsFileModel = this.fsFileService
					.insert(uploadFileNotSaveDb(fileUpModel));
		}
		logger.debug("上传文件 end");
		return fsFileModel;
	}
	
	@Override
	public List<FsFileModel> uploadFiles(List<FileUpModel> fileUpModels) throws Exception {
		if(fileUpModels==null || fileUpModels.size()==0){
			return null;
		}
		List<FsFileModel> fsFileModels = new ArrayList<FsFileModel>();
		for (FileUpModel fileUpModel : fileUpModels) {
			fsFileModels.add(uploadFile(fileUpModel));
		}
		return fsFileModels;
	}

	@Override
	public FileGroupModel uploadFileToGroup(List<FileUpModel> fileUpModels,
			Long idFileGroup, Boolean cleanGroupFile) throws Exception {
		logger.debug("上传文件 start");
		List<FsFileModel> fsFileModels = new ArrayList<FsFileModel>();
		for (FileUpModel fileUpModel : fileUpModels) {
			FsFileModel fs = uploadFileNotSaveDb(fileUpModel);
			fs.getFsConfigModel().setAttrsJson(null);
			fsFileModels.add(fs);
		}
		FileGroupModel fileGroupModel = this.fileGroupService.insert(
				fsFileModels, idFileGroup, cleanGroupFile);
		logger.debug("上传文件 end");
		return fileGroupModel;
	}

	@Override
	public FileDownModel downLoadFile(Long idFsFile) throws Exception {
		FileDownModel result = null;
		FsFileModel fsFileModel = this.fsFileService.getFsFileModel(idFsFile);
		if (fsFileModel != null) {
			result = new FileDownModel();
			/**
			 * web方式下载文件
			 */
			if (FsTypeEnum.WEB.name().equals(
					fsFileModel.getFsConfigModel().getType())) {
				InputStream in = FileUtil.downLoadFile(fsFileModel
						.getFileWebPath());
				result.setFsFileModel(fsFileModel);
				result.setStream(in);
			}
		}
		return result;
	}

	@Override
	public boolean downLoadFile(Long idFsFile, File file) throws Exception {
		FileDownModel fileDownModel = this.downLoadFile(idFsFile);
		if (fileDownModel != null) {
			FrameUtil.copyInputStreamToFile(fileDownModel.getStream(), file);
			return true;
		}
		return false;
	}

	@Override
	public List<FsFileModel> getFsFilesByFileGroupId(Long idFileGroup)
			throws Exception {
		return this.fsFileService.getfsFileModelsByIdFileGroup(idFileGroup);
	}

	@Override
	public FsFileModel getFsFileByFsFileId(Long idFsFile) throws Exception {
		return this.fsFileService.getFsFileModel(idFsFile);
	}

	private FsFileModel uploadFileNotSaveDb(FileUpModel fileUpModel)
			throws Exception {
		FsFileModel fsFileModel = new FsFileModel();
		FsServiceModel fsServiceModel = this.fsServiceService
				.getFsServiceModelByType(fileUpModel.getFsEnum().name());

		fsFileModel.setIdFsConfig(fsServiceModel.getIdFsConfig());
		fsFileModel.setOriginalFileName(fileUpModel.getFilename());
		fsFileModel
				.setFileSize((long) fileUpModel.getInputStream().available());
		fsFileModel.setCreateTime(FrameUtil.getTime(null));
		/**
		 * web方式上传
		 */
		if (FsTypeEnum.WEB.name().equals(
				fsServiceModel.getFsConfigModel().getType())) {
			String fileAbsPath = filePathResolve.getFileAbsPath(fsServiceModel
					.getFsConfigModel().getRootPath(), FileUtil
					.getFileStuffByFileName(fileUpModel.getFilename()),
					fileUpModel.getFsEnum());
			fsFileModel.setFsConfigModel(fsServiceModel.getFsConfigModel());
			fsFileModel.setFilePath(fileAbsPath.substring(fsServiceModel
					.getFsConfigModel().getRootPath().length()));
			List<IUpWrap> inwrapList = new ArrayList<IUpWrap>();
			inwrapList.add(new com.yjd.comm.fs.file.model.InputStreamUpWrap(
					fileAbsPath, fileUpModel.getFilename(), fileUpModel
							.getInputStream()));
			FileUtil.upload(fsServiceModel.getFsConfigModel().getFsWebModel()
					.getUploadUrl(), inwrapList);
		}
		return fsFileModel;
	}

	@Override
	public void deleteGropFiles(Long idFileGroup, List<Long> idFsFileList)
			throws Exception {
		this.fileGroupRefService.deleteGropFiles(idFileGroup, idFsFileList);
	}

	/**
	 * 文件对象服务
	 */
	private IFsFileService fsFileService;
	/**
	 * 文件服务
	 */
	private IFsServiceService fsServiceService;
	/**
	 * 文件组服务
	 */
	private IFileGroupService fileGroupService;
	/**
	 * 文件组和文件关联表
	 */
	private IFileGroupRefService fileGroupRefService;
	/**
	 * 文件路径
	 */
	private IFilePathResolve filePathResolve;

	public IFsFileService getFsFileService() {
		return fsFileService;
	}

	public void setFsFileService(IFsFileService fsFileService) {
		this.fsFileService = fsFileService;
	}

	public IFsServiceService getFsServiceService() {
		return fsServiceService;
	}

	public void setFsServiceService(IFsServiceService fsServiceService) {
		this.fsServiceService = fsServiceService;
	}

	public IFileGroupService getFileGroupService() {
		return fileGroupService;
	}

	public void setFileGroupService(IFileGroupService fileGroupService) {
		this.fileGroupService = fileGroupService;
	}

	public IFilePathResolve getFilePathResolve() {
		return filePathResolve;
	}

	public void setFilePathResolve(IFilePathResolve filePathResolve) {
		this.filePathResolve = filePathResolve;
	}

	@Override
	public List<FsFileModel> getFsFileModels(List<Long> idFsFileList)
			throws Exception {
		return this.fsFileService.getFsFileModels(idFsFileList);
	}

}
